package cn.chenli.job.controller;

import cc.tuhaolicai.mt.predefined.database.PageInfo;
import cn.chenli.job.common.controller.Controller;
import pro.tools.data.text.ToolStr;

/**
 * @author chenli
 * @create 2017-08-11
 */
public class BaseController extends Controller {

    /**
     * 获取参数
     * @param param：
     */
    String getParam(String param){
        return getRequest().getParameter(param);
    }

    /**
     * 获取int型参数
     * @param param：
     */
    int getParamToInt(String param){
        String getParam = getParam(param);
        if (ToolStr.isBlank(getParam)) {
            return 0;
        }else{
            return Integer.parseInt(getParam);
        }
    }

    /**
     * 获取int型参数
     * @param param：
     * @param defaultParam:
     */
    int getParamToInt(String param,int defaultParam){
        String getParam = getParam(param);
        if (ToolStr.isBlank(getParam)) {
            return defaultParam;
        }else{
            return Integer.parseInt(getParam);
        }
    }

    /**
     * 获取分页信息
     */
    @SuppressWarnings("unchecked")
    public PageInfo getPageInfo() {
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(getParamToInt("pageIndex",1));
        pageInfo.setPageSize(getParamToInt("pageSize"));
        if (pageInfo.getPageSize() == 0) {
            pageInfo.setPageSize(20);
        }
        return pageInfo;
    }

    void setAttr(String key, Object o) {
        request.setAttribute(key,o);
    }
}